<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\Baidu;//Demo插件英文名，改成你的插件英文就行了
use cmf\lib\Plugin;
use think\Hook;
use think\Db;
use app\admin\model\RouteModel;

//Demo插件英文名，改成你的插件英文就行了
class BaiduPlugin extends Plugin
{

    public $info = [
        'name'        => 'Baidu',//Demo插件英文名，改成你的插件英文就行了
        'title'       => '百度推送',
        'description' => '百度推送',
        'status'      => 1,
        'author'      => 'mhys',
        'version'     => '1.0',
        'demo_url'    => 'http://demo.thinkcmf.com',
        'author_url'  => 'http://www.thinkcmf.com'
    ];

    public $hasAdmin = 0;//插件是否有后台管理界面

    public function run($params)
    {
         $this->portal_admin_after_save_article($params);
    }
    // 插件安装
    public function install()
    {
        return true;//安装成功返回true，失败false
    }

    // 插件卸载
    public function uninstall()
    {
        return true;//卸载成功返回true，失败false
    }

    //实现的footer_start钩子方法
    public function footerStart($param)
    {
        $config = $this->getConfig();
        $this->assign($config);
        echo $this->fetch('widget');
    }

    function portal_admin_after_save_article($params){
        $config =$this->getConfig();
        $wz=$config['text'];
        $token=$config['text2'];
        $urls=[];
        $routeModel = new RouteModel();
        if(isset($params['cid'])){
            foreach($params['cid'] as $key=>$value){
                $alias= $routeModel->getUrl('portal/List/index', ['id' =>$value]);
                $urls[$key]=$wz.'/'.$alias.'/'.$params['article'][$key].'.html';
            }
        }else{
            foreach($params['article'] as $key=>$value){
                $join = [['__PORTAL_CATEGORY_POST__ p', 'p.post_id=a.id ', 'left'],];
                $field='a.id,p.category_id';
                $category_id= Db::name('portal_post')->alias('a')->where('a.id',$value)->join($join)->field($field)->find();
                $alias= $routeModel->getUrl('portal/List/index', ['id' =>$category_id['category_id']]);
                $urls[$key]=$wz.'/'.$alias.'/'.$value.'.html';
            }
        }
            $api = 'http://data.zz.baidu.com/'.$params['type'].'?site='.$wz .'&token='.$token;
            $ch = curl_init();
            $options = array(
                CURLOPT_URL => $api,
                CURLOPT_POST => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => implode("\n", $urls),
                CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
            );
            curl_setopt_array($ch, $options);
            $result = curl_exec($ch);
        //print_r($result);
        $new_result=json_decode($result,true);
        $new_result['msg']="推送日志";$new_result['type']=$params['type'];$new_result['aurl']=$urls;$new_result['time']=date("Y-m-d H:i",time());
        if($result){\Think\Log::write($new_result,'log ');}
        return $result;
    }


}